package com.ash.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.ash.domain.User;
import com.ash.domain.vo.UserVo;
import com.ash.service.user.UserInfoService;
import com.ash.service.user.UserService;
import com.ash.util.CodeUtil;
import com.ash.util.JsonUtil;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/3/20 11:43
 * @file : project06.com.ash.controller.user.UserController.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/code")
    @ResponseBody
    public void code(HttpServletResponse res, HttpSession session) throws IOException {
        res.setContentType("image/jpg");
        String code = CodeUtil.createCode(5);
        session.setAttribute("code", code);
        boolean img = ImageIO.write(CodeUtil.creatImage(150, 40, code),
                "jpg", res.getOutputStream());
        if (img) {
            System.out.println("验证码生成成功！");
        } else {
            System.out.println("验证码生成失败！");
        }
    }



    @RequestMapping("/checkRegisterName")
    @ResponseBody
    public String checkRegisterName(String uname) {
        User user = userService.selectNameToRegister(uname);
        return user == null ? "ok" : "exist";
    }



    @RequestMapping("/user/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/jsp/login.jsp";
    }

    @RequestMapping("/user/userEdit")
    public String userEdit() {
        return "admin/userEdit";
    }

    @RequestMapping("/user/selectAll")
    @ResponseBody
    public String selectAll(@Nullable @RequestParam("uname") String uname, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        /*查询数据*/
        List<UserVo> users = userService.selectAll(uname, page, limit);
        int count = userService.dataCount(uname);
        /*封装数据并返回*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", users);
        jsonObject.put("count", count);
        return jsonObject.toJSONString();
    }

    @RequestMapping("/user/delete")
    @ResponseBody
    public String delete(int uid) {
        return JsonUtil.getJsonByObject(userService.delete(uid));
    }

    @RequestMapping("/user/main")
    public String main() {
        return "main";
    }

    @PostMapping("/user/deleteByIds")
    @ResponseBody
    public Integer deleteByIds(@RequestParam("ids") List<Integer> ids) {
        return userService.deleteByIds(ids);
    }

    @RequestMapping("/user/update")
    public void update(User user) {
        System.out.println(user);
        if(user.getUid()!=null){
            userService.update(user);
        }
    }

    @RequestMapping("/user/passwordUpdate")
    public String passwordUpdate(){
        return "user/passwordUpdate";
    }
    @RequestMapping("/user/checkPassword")
    @ResponseBody
    public Integer checkPassword(User user,@RequestParam("oldPassword") String oldPassword){
        User user1 = new User();
        user1.setPassword(oldPassword);
        user1.setUname(user.getUname());
        user1=userService.selectToLogin(user1);
        return user1==null?1:0;
    }
    @PostMapping("/user/changePassword")
    @ResponseBody
    public Integer changePassword(User user,@RequestParam("oldPassword") String oldPassword){
        User user1 = new User();
        user1.setPassword(oldPassword);
        user1.setUname(user.getUname());
        user1=userService.selectToLogin(user1);
        System.out.println(user1);
        if(user1==null){
            return 0;
        }
        else {
            user.setUid(user1.getUid());
            return userService.update(user);
        }
    }
}
